package com.swlz.service.impl;

import java.sql.SQLException;

import com.swlz.dao.FictionDAO;
import com.swlz.entity.Fiction;
import com.swlz.exception.CodeException;
import com.swlz.service.FictionService;
import com.swlz.vo.PageVO;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 小说
 * ClassName : com.swlz.service.impl.FictionServiceImpl
 * Author : swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-07-01 14:29:58
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class FictionServiceImpl implements FictionService {
	
	private FictionDAO fictionDAO = new FictionDAO();

	@Override
	public int add(Fiction fiction) throws CodeException {
		try {
			return fictionDAO.insertSelective(fiction);
		} catch (SQLException e) {
			throw new CodeException(500, "添加小说错误",e);
		}
	}

	@Override
	public int update(Fiction fiction) throws CodeException {
		try {
			return fictionDAO.updateSelective(fiction);
		} catch (SQLException e) {
			throw new CodeException(501, "修改小说错误",e);
		}
	}

	@Override
	public int delete(Long fictionId) throws CodeException {
		try {
			return fictionDAO.deleteById(fictionId);
		} catch (SQLException e) {
			throw new CodeException(502, "删除小说错误",e);
		}
	}

	@Override
	public Fiction get(Long fictionId) throws CodeException {
		try {
			return fictionDAO.selectById(fictionId);
		} catch (SQLException e) {
			throw new CodeException(503, "获取小说失败",e);
		}
	}

	@Override
	public PageVO<Fiction> page(Fiction fiction, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = fictionDAO.countByModel(fiction);
			PageVO<Fiction> page = new PageVO<>(total, current, rowMax);
			return page.setList(fictionDAO.selectByModel(fiction, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			throw new CodeException(504, "获取小说分页数据失败",e);
		}
	}


}
